package com.web.sys.sercice;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.inject.Inject;

import org.springframework.stereotype.Service;

import com.web.sys.dao.ISysUserDao;
import com.web.sys.dto.UserQueryDTO;
import com.web.sys.model.SysUser;
import com.web.sys.model.SysUserRole;
import com.web.sys.util.MD5Util;

@Service("sysUserService")
public class SysUserService implements ISysUserService {
	
	@Inject
	private ISysUserDao sysUserDao;

	@Override
	public SysUser add(SysUser user) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void delete(String id) {
		// TODO Auto-generated method stub

	}

	@Override
	public void update(SysUser user) {
		// TODO Auto-generated method stub

	}

	@Override
	public SysUser load(String username) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<SysUser> queryAllSysUser() {
		List<SysUser> userList=sysUserDao.list("from SysUser");
		return userList;
	}

	@Override
	public List<SysUser> queryUserform(UserQueryDTO dto) {
		String hql="from SysUser where 1=1 ";
		StringBuffer sb=new StringBuffer("");
		List<Object> params=new ArrayList<Object>();
		if(null!=dto){
			if(null!=dto.getUsername() && !"".equals(dto.getUsername())){
				sb.append(" and username=? ");
				params.add(dto.getUsername());
			}
			if(null!=dto.getNickname() && !"".equals(dto.getNickname())){
				sb.append(" and nickname=? ");
				params.add(dto.getNickname());
			}
			if(null!=dto.getStatus() && !"".equals(dto.getStatus())){
				sb.append(" and status=? ");
				params.add(dto.getStatus());
			}
			if(null!=dto.getCreatedate() && !"".equals(dto.getCreatedate())){
				sb.append(" and createdate=? ");
				params.add(dto.getCreatedate());
			}
		}
		hql+=sb.toString();
		List<SysUser> userList=sysUserDao.list(hql,params.toArray());
		return userList;
	}

	@Override
	public String saveUserform(UserQueryDTO dto) {
		try {
			SysUser u=new SysUser();
			u.setNickname(dto.getNickname());
			String pwd=MD5Util.md5(dto.getUsername(), "a");
			u.setPassword(pwd);
			u.setUsername(dto.getUsername());
			u.setCreatedate(dto.getCreatedate());
			sysUserDao.add(u);
			return null;
		} catch (Exception e) {
			e.printStackTrace();
			return "err";
		}
	}

	@Override
	public SysUser queryUserByName(String username) {
		try {
			SysUser user=(SysUser)sysUserDao.queryObject("from SysUser where username=?",username);
			return user;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	@Override
	public String updateUserform(UserQueryDTO dto) {
		try {
			String username=dto.getUsername();
			SysUser u=(SysUser)sysUserDao.queryObject("from SysUser where username=?",username);
			u.setNickname(dto.getNickname());
			u.setCreatedate(dto.getCreatedate());
			u.setStatus(dto.getStatus());
			sysUserDao.update(u);
			return null;
		} catch (Exception e) {
			e.printStackTrace();
			return "err";
		}
	}

	@Override
	public String deleteUserByName(String username) {
		try {
			int n=sysUserDao.deleteByConditionHql("delete from SysUser where username=?", username);
			if(n>0){
				return null;
			}else{
				return "err";
			}
		} catch (Exception e) {
			e.printStackTrace();
			return "err";
		}
	}

	@Override
	public String queryUserRole(String username) {
		SysUserRole userRole=(SysUserRole)sysUserDao.queryObject("from SysUserRole where username=?",username);
		return userRole.getRolecode();
	}

}
